Sh.User.Typing = new Class({
	initialize : function (user) {
		this.user = user;
		this.catchEnter();
	},
	catchEnter : function () {
		var input = this.user.input;
		$(document).keypress(function (e) {
				// fucking opera
				if ($.keyboard.getKey(e) != 'enter') {
					return true;
				}

				var text = input.getValue();
				try {
					if (this.isOpen(text)) {
						input.addValue('\n', 'max');
					} else {
						this.user.sendCommand(text);
						input.clear()
					}
				} catch (e) {
					console.error(e);
				}
				return false;
			}.bind(this)
		);
	},
	isOpen : function (text) {
		if (!text) {
			return false;
		}
		var parse = new Sh.Parser.UserText(text)
			.get();
		return parse.getLast().type == 'escape'
			|| parse.getLast().open;
	}
});

